package com.yxy.service;

import com.github.pagehelper.PageHelper;
import com.yxy.base.BusinessException;
import com.yxy.dao.PersonMapper;
import com.yxy.dao.RoleMapper;
import com.yxy.dto.RoleDto;
import com.yxy.feign.VO.AdminUserInToken;
import com.yxy.model.Role;
import com.yxy.utils.TenantHelper;
import com.yxy.utils.TokenCheckUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Slf4j
@Service
public class RoleService {


  @Resource
  private HttpServletRequest request;

  @Autowired
  private TokenCheckUtil tokenCheckUtil;

  @Resource
  private PersonMapper personMapper;
  @Resource
  private RoleMapper roleMapper;







  @Autowired
  private TenantHelper tenantHelper;


  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public Role save(RoleDto pojo) throws BusinessException {
    AdminUserInToken adminUser = tokenCheckUtil.getUserInfoByToken();

    Role entity = new Role();
    BeanUtils.copyProperties(pojo, entity);
    entity.setCreateDate(new Date());
    entity.setCreateUserId(adminUser.getAdminUser().getId());
    entity.setCreateUserName(adminUser.getAdminUser().getUsername());
    roleMapper.insertSelective(entity);
    return entity;
  }








  //编辑数据
  @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
  public Role modify(RoleDto pojo) throws BusinessException {
    Role gpfMarketProductcode = roleMapper.selectByPrimaryKey(pojo.getId());
    if (gpfMarketProductcode == null) {
      throw new BusinessException(501, "修改数据失败，未找到Id为[" + pojo.getId() + "]的数据");
    }

    AdminUserInToken adminUser = tokenCheckUtil.getUserInfoByToken();
    Role entity = new Role();
    BeanUtils.copyProperties(pojo, entity);
    entity.setUpdateDate(new Date());
    try {
      entity.setUpdateUserId(adminUser.getAdminUser().getId());
    } catch (NullPointerException ne) {
      entity.setUpdateUserId(Long.parseLong("3"));
    }
    try{
      entity.setUpdateUserName(adminUser.getAdminUser().getUsername());
    }catch (NullPointerException ne){
      entity.setUpdateUserName("admin");
    }
    roleMapper.updateByPrimaryKeySelective(entity);
    return entity;
  }






  //获取加工企业登记审核列表
  public List<Role> getlist(RoleDto query) throws BusinessException {
    AdminUserInToken adminUser = tokenCheckUtil.getUserInfoByToken();
    // 处理tenant id list
    PageHelper.startPage(query.getPageNumber(), query.getPageSize());
    PageHelper.orderBy("t1.create_date desc");//排序
    List<Role> dbResult = roleMapper.selectList(query);
    return dbResult;
  }

  //获取加工企业登记审核列表
  public Role getRoleInfoByName(RoleDto query) throws BusinessException {
    AdminUserInToken adminUser = tokenCheckUtil.getUserInfoByToken();
    // 处理tenant id list
    Role dbResult = roleMapper.getRoleInfoByName(query);
    return dbResult;
  }









}
